package vip.xiaonuo.sys.modular.monitoringgather.config;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class ImportExcelListener extends AnalysisEventListener<Object> {
    // 封装数据
    private List<Object> list = new ArrayList<>();
    // sheet页索引
    private int sheetNo = 0;

    @Override
    public void invoke(Object t, AnalysisContext context) {
        // 读取Excel内容
        int currentSheetNo = context.readSheetHolder().getSheetNo();
        if(currentSheetNo != sheetNo){
            // 根据sheet页索引,防止重复添加数据
            list = new ArrayList<>();
            sheetNo = currentSheetNo;
        }
        list.add(t);
    }

    /**
     * 将表格转化为map集合（复杂Excel）
     */
    public List<LinkedHashMap> getListMap(){
        String jsonObj = JSON.toJSONString(list);
        return JSON.parseArray(jsonObj,LinkedHashMap.class);
    }

    /**
     * 读取完数据后的操作
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context){

    }
}
